/*
 * Copyright (c) 2012-2013, Charles Santos da Silva <silva.charlessantos@gmail.com> -
 * scharlessantos.org
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE
 */

package org.scharlessantos.hermes.logger;

import org.scharlessantos.hermes.HConfiguration;
import org.scharlessantos.hermes.writer.HLevel;
import org.scharlessantos.hermes.writer.HWriter;

/**
 * Generic Console log Writer for general purpose <br>
 * Hermes is free open-source Java Logger.<br>
 * It receive this name because Hermes was the messenger of Olympians gods, so,
 * anybody better than Hermes to send system messages to programmer.<br>
 * <br>
 * * <br>
 * Hermes is licensed by new BSD License. A notice of license can be saw below<br>
 * <br>
 * <i>
 * Copyright (c) 2012-2013, Charles Santos da Silva <silva.charlessantos@gmail.com> -
 * scharlessantos.org
 * All rights reserved.<br>
 * <br>
 * Redistribution and use in source and binary forms, with or without<br>
 * modification, are permitted provided that the following conditions are met:<br>
 * <br>
 * 1. Redistributions of source code must retain the above copyright notice,<br>
 * this list of conditions and the following disclaimer.<br>
 * 2. Redistributions in binary form must reproduce the above copyright<br>
 * notice, this list of conditions and the following disclaimer in the<br>
 * documentation and/or other materials provided with the distribution.<br>
 * <br>
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"<br>
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE<br>
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE<br>
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE<br>
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR<br>
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF<br>
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS<br>
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN<br>
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)<br>
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE<br>
 * POSSIBILITY OF SUCH DAMAGE<br>
 * </i>
 * 
 * @author Charles Santos da Silva
 */
public class HSysoutLogger implements HWriter {
	
	private static HSysoutLogger log;
	
	/**
	 * Gets an instance of Hermes Generic Console Logger
	 */
	public static HWriter getInstance() {
		if (log == null)
			log = new HSysoutLogger();
		
		return log;
	}
	
	@Override
	public void logMessage(HLevel level, StackTraceElement source, String message) {
		if (level == HLevel.FATAL)
			System.err.println(String.format(HConfiguration.getInstance().getBaseFormat() + ": %s", level.name(), source.getClassName(), source.getMethodName(), source.getFileName(), source.getLineNumber(), Thread.currentThread().getId(), message));
		else
			System.out.println(String.format(HConfiguration.getInstance().getBaseFormat() + ": %s", level.name(), source.getClassName(), source.getMethodName(), source.getFileName(), source.getLineNumber(), Thread.currentThread().getId(), message));
		
	}
	
	@Override
	public void logException(HLevel level, StackTraceElement source, Throwable exception) {
		if (level == HLevel.FATAL) {
			System.err.println(String.format(HConfiguration.getInstance().getBaseFormat() + ": Exception", level.name(), source.getClassName(), source.getMethodName(), source.getFileName(), source.getLineNumber(), Thread.currentThread().getId()));
			exception.printStackTrace(System.err);
		} else {
			System.out.println(String.format(HConfiguration.getInstance().getBaseFormat() + ": Exception", level.name(), source.getClassName(), source.getMethodName(), source.getFileName(), source.getLineNumber(), Thread.currentThread().getId()));
			exception.printStackTrace(System.out);
		}
	}
	
	@Override
	public int hashCode() {
		return 0;
	}
	
	@Override
	public boolean equals(Object obj) {
		return obj.getClass().getName().equals(getClass().getName());
	}
	
}
